<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { parseTime } from '@/utils/ruoyi.js'
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '420px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler (val) {
        const chartData = JSON.parse(JSON.stringify(val))
        this.setOptions(chartData.reverse())
      }
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy () {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart () {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions (data) {
      const category = data.map(item => this.parseTime(item.date, '{y}-{m}-{d}'));
      const values = data.map(item => item.value);
      console.log(category)
      this.chart.setOption({
        xAxis: {
          type: 'value',
          show: false,
          boundaryGap: [0, 0.01]
        },
        grid: {
          left: '3%',
          right: '5%',
          bottom: '3%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10],
          formatter: function (params) {
            const current = data[params[0].dataIndex]
            const date  = parseTime(current.date, '{y}-{m}-{d}')
            var tooltipHtml = '';
            tooltipHtml += '<div>';
            tooltipHtml += '<p style="margin:0;">'+ date + '</p>';
            tooltipHtml += '<p style="margin:0;">' + current.title+'<span style="margin-left: 20px;">'+current.value+'</span>'+'</p>';
            tooltipHtml += '</div>';
            return tooltipHtml;
          }
        },
        yAxis: {
          type: 'category',
          axisLine: {
            show: false, //不显示坐标轴线
          },
          axisTick: {
            show: false //不显示坐标轴刻度线
          },
          axisLabel: {
            show: true,
            width: 100,
            textStyle: {
              color: '#fff'
            }
          },
          data: category
        },
        series: [
          {
            type: 'bar',
            showBackground: true,
            barWidth: "6",
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                { offset: 0, color: '#1A2846' },
                { offset: 0.5, color: '#1D7194' },
                { offset: 1, color: '#21C5F3' }
              ])
            },
            data: values
          }
        ]
      })
    }
  }
}
</script>
